﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;

public partial class thanhtoan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataConnection.str = @"Data Source=xe;User ID=ODER;Password=12345";
        MultiView1.ActiveViewIndex = 0;
        if (!IsPostBack)
        {
            int tongtien = 0;
            DataTable dt = (DataTable)Session["GioHang"];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                tongtien = tongtien + int.Parse(dt.Rows[i]["dongia"].ToString()) * int.Parse(dt.Rows[i]["soluong"].ToString());
                lbtongtien.Text = "Tổng số tiền: " + tongtien.ToString() + " VNĐ";
            }
        }
       

    }

   
    protected void btndathang_Click(object sender, EventArgs e)
    {

        MultiView1.ActiveViewIndex = 1;

        string tennguoinhan = txtnguoinhan.Text;
        string diachi = txtdiachi.Text;
        string sdt = txtsodt.Text;
        string tendn = Session["TenDN"].ToString();
        DateTime ngaydat = DateTime.Now;
        DateTime ngaygiao;
        int tongtien = 0;
        int cuoc = 0;
        DataTable dt = (DataTable)Session["GioHang"];
        
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            tongtien = tongtien + int.Parse(dt.Rows[i]["dongia"].ToString()) * int.Parse(dt.Rows[i]["soluong"].ToString());

        }
        if (RadioButtonList1.SelectedIndex == 0)
        {
            ngaygiao = ngaydat.AddDays(7);
        }
        else
        {
            if (RadioButtonList1.SelectedIndex == 1)
            {
                ngaygiao = ngaydat.AddDays(2);
                cuoc = 20000;
            }
            else
            {
                ngaygiao = ngaydat;
                cuoc = 50000;
            }
        }

        // Get Max DonHang
        DataTable dtddh = DataConnection.GetDatatableByStoreProcedureNameNoParam("NGOCTRAN.sp_getCountDonHang");
        int maddh = int.Parse(dtddh.Rows[0]["sodondathang"].ToString()) + 1;
        int tinhtrang = 0;
        //string tinhtrang = "False";
        OracleParameter[] thamso = new OracleParameter[]
        {
            new OracleParameter("P_MADONHANG",maddh),
            new OracleParameter("P_TENDN",tendn),
            new OracleParameter("P_NGAYDATHANG",ngaydat),
            new OracleParameter("P_TONGGIATRIDONHANG",tongtien),
            new OracleParameter("P_DIACHIGIAO",diachi),
            new OracleParameter("P_NGAYGIAO",ngaygiao),
            new OracleParameter("P_CUOC",cuoc),
            new OracleParameter("P_TINHTRANG",tinhtrang),
            new OracleParameter("P_TENUSERS",tennguoinhan),
            new OracleParameter("P_SDT",sdt),
        };

        DataConnection.getPara("NGOCTRAN.insertDonHang", thamso);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string masach = dt.Rows[i]["masach"].ToString();
            int soluong = int.Parse(dt.Rows[i]["soluong"].ToString());
            int gia = int.Parse(dt.Rows[i]["soluong"].ToString()) * int.Parse(dt.Rows[i]["dongia"].ToString());

           OracleParameter[] thamso1 = new OracleParameter[] { 
                new OracleParameter("P_MASACH",masach),
                new OracleParameter("P_MADONHANG",maddh),
                new OracleParameter("P_SOLUONG",soluong),
                new OracleParameter("P_GIA",gia)
               
            };
           DataConnection.getPara("insertChiTietDonHang", thamso1);
        }
        dt.Rows.Clear();
        Session["GioHang"] = null;
    }


    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int tongtien = 0;
        DataTable dt = (DataTable)Session["GioHang"];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            tongtien = tongtien + int.Parse(dt.Rows[i]["dongia"].ToString()) * int.Parse(dt.Rows[i]["soluong"].ToString());
        }
        if (RadioButtonList1.SelectedIndex == 1)
        {
            tongtien = tongtien + 20000;
            lbtongtien.Text = "Tổng số tiền: " + tongtien.ToString() + " VNĐ";
        }
        else
        {
            if (RadioButtonList1.SelectedIndex == 2)
            {
                tongtien = tongtien + 50000;
                lbtongtien.Text = "Tổng số tiền: " + tongtien.ToString() + " VNĐ";
            }
            else
            {
                lbtongtien.Text = "Tổng số tiền: " + tongtien.ToString() + " VNĐ";
            }
        }
    }

    protected void txtnguoinhan_TextChanged(object sender, EventArgs e)
    {

    }
}